<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

class m_website_actions_block_MoveMenuAction extends m_backoffice_extends_FormModal
{
	public function execute()
	{
		$menu = $this->getDocumentProvider()->getByUniqueId($this->getRequestParameter("docId"), $this->getRequestParameter("docLang"));
		$formValues = $this->getFormValues("form");
		$relManager = f_relation_Manager::getInstance();
		$parent = $relManager->getUniqueParent($menu->getUniqueId(), $menu->getLang());

        $folders = m_backoffice_services_SelectService::getInstance()->setRootNodeById($this->getContextWebsite()->getUniqueId())
                                                                     ->setFolderModule('website')
                                                                     ->unsetId($menu->getUniqueId())
                                                                     ->disableId($parent->getUniqueId())
                                                                     ->execute();

		if(count($formValues) > 0)
		{
			$destMenu = $this->getDocumentProvider()->getByUniqueId($formValues["folder"], $menu->getLang());
			
			$relManager->clearRelation($parent, $menu, "menus", $menu->getLang());
			$relManager->setRelation($destMenu, $menu, "menus", null, $menu->getLang());
			
			$errors["status"] = "success";
			$this->addToModel("formValues", $formValues);
			$this->addToModel("errors", $errors);
		}
		
		$this->addToModel("href", $this->getRequestParameter("href"));
        $this->addToModel('hasValues', $folders->getSelectableItemsNb() > 0);
		$this->addToModel("folders", $folders->getSelectContent());
		$this->addToModel("currentFolderId", $menu->getDocumentParent()->getUniqueId());
		$this->addToModel("menu", $menu);
		$this->setView('success');
	}	
	
	public function validate()
	{
		return $this->getUserContext()->hasRight(array("backoffice_accessBo", "website_Edit"));
	}		
}